//
//  LZRestaurantCellButton.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/11/16.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZRestaurantCellButton.h"
#import "Masonry.h"

@implementation LZRestaurantCellButton

-(instancetype)init {
    self = [super init];
    if (self) {
        self.layer.cornerRadius = 4;
        self.layer.borderWidth = 0.5;
        [self createSubView];
    }
    return self;
}

-(void)createSubView {
    
    self.imgView = [[UIImageView alloc] init];
    [self addSubview:self.imgView];
    
    self.label = [[UILabel alloc] init];
    self.label.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.label];
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.leading.equalTo(self).offset(5);
    }];
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.leading.equalTo(self.imgView.mas_trailing).offset(5);
    }];
}

- (void)setContent:(NSString*)aString andColor:(UIColor*)color andImage:(UIImage*)aImage {
    if (aString.length == 0) {
        [self.imgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self).offset(0);
        }];
    }
    else {
        [self.imgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self).offset(5);
        }];
    }
    self.label.text = aString;
    self.label.textColor = color;
    self.imgView.image = aImage;
    self.layer.borderColor = color.CGColor;
}

-(CGSize)intrinsicContentSize {
    
    if (self.label.text.length == 0) {
        return CGSizeMake([self.imgView intrinsicContentSize].width, [self.imgView intrinsicContentSize].height);
    }
    else {
        CGSize size1 = [self.label intrinsicContentSize];
        CGSize size2 = [self.imgView intrinsicContentSize];
        
        return CGSizeMake(size1.width+size2.width+15, 29);
    }
}

@end
